package components.managers
{
    import components.contorls.superClass.BaseToolTip;
    import components.core.zn_internal;
    import components.utils.EventListenerUtil;
    
    import flash.geom.Point;
    import flash.utils.getDefinitionByName;
    
    import mx.events.ResizeEvent;

    public class ToolTipManager
    {
        public static var defaultToolTipClass:Class = null

        private static var _currentToolTip:BaseToolTip;
		
		private static var _mousePoint:Point;

        public function ToolTipManager()
        {

        }

        public static function createToolTip(data:Object, p:Point,toolTipClass:Class=null):void
        {
			
			destroyToolTip();
			
            if (toolTipClass == null)
            {
                if (defaultToolTipClass == null)
					toolTipClass = Class(getDefinitionByName("components.contorls.ToolTip"));
                else
					toolTipClass = defaultToolTipClass;
            }
			
			_mousePoint=p;

            _currentToolTip = new toolTipClass();

            _currentToolTip.data = data;

			var app:SystemManager = SystemManager.instance;
			
			if (app)
				app.zn_internal::showToolTip(_currentToolTip);
			
			_currentToolTip.validateNow();
			toolTipsSizeChange(null);
			EventListenerUtil.addEventListener(_currentToolTip,ResizeEvent.RESIZE,toolTipsSizeChange);
        }

		private static function toolTipsSizeChange(event:ResizeEvent):void
		{
			var app:SystemManager = SystemManager.instance;
			
			_currentToolTip.x = _mousePoint.x;
			_currentToolTip.y = _mousePoint.y;
			
			_currentToolTip.y = _mousePoint.y - currentToolTip.height;
			
			if (_mousePoint.x + _currentToolTip.width > app.width)
				_currentToolTip.x = _mousePoint.x - _currentToolTip.width;
			
			//            if (point.y + currentToolTip.height > app.height)
			//                currentToolTip.y = point.y - currentToolTip.height;
			
			if (_mousePoint.y < 0)
				_currentToolTip.y = _mousePoint.y;
		}
		
        public static function destroyToolTip():void
        {
            var app:SystemManager = SystemManager.instance;

            if (app)
                app.zn_internal::hideToolTip();
			
			if(_currentToolTip)
				_currentToolTip.destroy();
			_currentToolTip = null;
        }

        public static function get currentToolTip():BaseToolTip
        {
            return _currentToolTip;
        }
    }
}
